package com.fyl.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class PageQueryWrappers {

    public static <T> QueryWrapper<T> pageQuery(Map<String, Object> params) {
        Map<String, Object> filters = PageQueryWrappers.getSearchParams(params);
        return PageQueryWrappers.buildQueryWrapper(filters);
    }


    private static final String PAGE_QUERY_PREFIX = "PQ_";

    private static Map<String, Object> getSearchParams(Map<String, Object> params) {
        Map<String, Object> filters = new HashMap<>();
        for (String key : params.keySet()) {
            if (StringUtils.startsWith(key, PAGE_QUERY_PREFIX)) {
                String name = StringUtils.substringAfter(key, PAGE_QUERY_PREFIX);
                Object value = params.get(key);
//                try {
//                    String decode = URLDecoder.decode(value.toString(), StandardCharsets.UTF_8.name());
//                    filters.put(name, decode);
//                } catch (UnsupportedEncodingException e) {
//                    e.printStackTrace();
//                }
                filters.put(name, value);
            }
        }
        return filters;
    }

    private static <T> QueryWrapper<T> buildQueryWrapper(Map<String, Object> filters) {

        QueryWrapper<T> queryWrapper = new QueryWrapper<>();

        for (Map.Entry<String, Object> entry : filters.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            String[] s = StringUtils.split(key, "_");
            String column = s[0].replaceAll("[A-Z]", "_$0").toLowerCase();//驼峰 转 下划线
            String operator = s[1].toUpperCase();//转大写

            switch (operator) {
                case "EQ":
                    queryWrapper.eq(null != value, column, value);
                    break;
                case "NE":
                    queryWrapper.ne(null != value, column, value);
                    break;
                case "LIKE":
                    queryWrapper.like(null != value, column, value);
                    break;
                case "LIKER":
                    queryWrapper.likeRight(null != value, column, value);
                    break;
                case "LIKEL":
                    queryWrapper.likeLeft(null != value, column, value);
                    break;
                case "GT":
                    queryWrapper.gt(null != value, column, value);
                    break;
                case "LT":
                    queryWrapper.lt(null != value, column, value);
                    break;
                case "GE":
                    queryWrapper.ge(null != value, column, value);
                    break;
                case "LE":
                    queryWrapper.le(null != value, column, value);
                    break;
                case "IN":
                    if (null != value) {
                        String valueString = value.toString();
                        queryWrapper.inSql(StringUtils.isNotBlank(valueString), column, "'" + valueString.replace(",", "','") + "'");
                    }
                    break;
                case "ISNULL":
                    queryWrapper.isNull(column);
                    break;
                case "NOTNULL":
                    queryWrapper.isNotNull(column);
                    break;
                default:
            }
        }
        return queryWrapper;
    }

}
